package com.hdsx.restful.glzt.roadline.nclx.rest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.wink.common.annotations.Workspace;
import org.springframework.stereotype.Service;

import com.hdsx.restful.dist.service.IDistService;
import com.hdsx.restful.glzt.roadline.lxxx.bean.Line;
import com.hdsx.restful.glzt.roadline.lxxx.bean.LxAreaDetail;
import com.hdsx.restful.glzt.roadline.lxxx.bean.LxDetail;
import com.hdsx.restful.glzt.roadline.nclx.bean.Nclx;
import com.hdsx.restful.glzt.roadline.nclx.service.INclxService;
import com.hdsx.restful.glzt.structure.crk.service.CrkServer;
import com.hdsx.restful.glzt.structure.fwq.service.FwqServer;
import com.hdsx.restful.glzt.structure.jkss.service.JkssService;
import com.hdsx.restful.glzt.structure.jtlgcz.service.JtlgczServer;
import com.hdsx.restful.glzt.structure.kbqbb.service.KbqbbServer;
import com.hdsx.restful.glzt.structure.ql.service.QlServer;
import com.hdsx.restful.glzt.structure.sd.service.SdServer;
import com.hdsx.restful.glzt.structure.sfz.service.SfzServer;
import com.hdsx.restful.glzt.structure.zcz.service.ZczServer;
import com.hdsx.restful.util.CheckEmpty;
import com.hdsx.restful.util.EasyUI;
@Workspace(workspaceTitle = "restful", collectionTitle = "glzt")  
@Service
@Path("/nclx")
public class NclxRestService {
	
	@Resource(name="nclxServiceImpl")
	private INclxService server;

	@Resource(name="distServiceImpl")
	private IDistService distService;
	
	@Resource(name="crkServerImpl")
	private CrkServer crkServer;
	
	@Resource(name="fwqServerImpl")
	private FwqServer fwqServer;
	
	@Resource(name="jkssServiceImpl")
	private JkssService jkssService;
	
	@Resource(name="jtlgczServerImpl")
	private JtlgczServer jtlgczServer;
	
	@Resource(name="kbqbbServerImpl")
	private KbqbbServer kbqbbServer;
	
	@Resource(name="qlServerImpl")
	private QlServer qlServer;
	
	@Resource(name="sdServerImpl")
	private SdServer sdServer;
	
	@Resource(name="sfzServerImpl")
	private SfzServer sfzServer;
	
	@Resource(name="zczServerImpl")
	private ZczServer zczServer;
	/**
	 * Gets the list.
	 * http://192.168.0.137:8080/restful/rest/nclx/list?lddm=Z001610624&page=0&rows=10
	 * @param
	 * @param
	 * @return the list
	 */
	@GET
	@Path("/list")
	@Produces(MediaType.APPLICATION_JSON)
	public EasyUI<Nclx> queryList(@QueryParam("lxdm") String lxdm,@QueryParam("xzdj")String xzdj,
			@QueryParam("xzqhbm")String xzqhbm,@QueryParam("name") String name,@QueryParam("sxxfx")String sxxfx_string,
			@QueryParam("page") int page,@QueryParam("rows") int rows) {
		EasyUI<Nclx> uiPage = new EasyUI<Nclx>(page, rows);
		Map<String,Object> map = uiPage.getParameter();
		Integer sxxfx = null;
		try{
			sxxfx = Integer.parseInt(sxxfx_string);
		}catch(Exception e){
			sxxfx = null;
		}
		xzqhbm = CheckEmpty.subXzqhbm(xzqhbm);
		map.put("lxdm", lxdm);
		map.put("xzdj", xzdj);
		map.put("xzqhbm", xzqhbm);
		map.put("sxxfx", sxxfx);
		map.put("name", name);
		int count =server.queryCount(map);
		List<Nclx> servicefws= server.queryList(map);
		uiPage.setTotal(count);
		uiPage.setRows(servicefws);
		return uiPage;
	}
	
	/**
	 * 桥梁，隧道，收费站，服务区，出入口，可变情报板，交通量观测站，监控摄像设备
	 * @param id
	 * @param type
	 * @return
	 */
	@GET
	@Path("/detail")
	@Produces(MediaType.APPLICATION_JSON)
	public LxDetail detail(@QueryParam("id")String id){
		LxDetail lxDetail = new LxDetail();
		Line line = new Line();
		Nclx nclx = server.queryById(id);
		try{
			PropertyUtils.copyProperties(line, nclx);
			line.setLxdm(nclx.getLddm());
		}catch(Exception e){
			e.printStackTrace();
		}
		List<LxAreaDetail> areas = new ArrayList<LxAreaDetail>();
		lxDetail.setLine(line);
		lxDetail.setAreas(areas);
		if(!CheckEmpty.isEmptyString(line.getXzqhbm()))
			lxDetail.setArea(distService.selectNameByCode(line.getXzqhbm()));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("lxbm", line.getLxdm());
		map.put("sxxfx", line.getSxxfx());
		map.put("xzqhbm", line.getXzqhbm());
		map.put("qdzh", line.getQdzh());
		map.put("zdzh", line.getZdzh());
		lxDetail.setCrkNum(crkServer.countTotal(map));
		lxDetail.setFwqNum(fwqServer.countTotal(map));
		lxDetail.setJksxsbNum(jkssService.countTotal(map));
		lxDetail.setJtlgczNum(jtlgczServer.countTotal(map));
		lxDetail.setKbqbbNum(kbqbbServer.countTotal(map));
		lxDetail.setQlNum(qlServer.countTotal(map));
		lxDetail.setSdNum(sdServer.countTotal(map));
		lxDetail.setSfzNum(sfzServer.countTotal(map));
		lxDetail.setZczNum(zczServer.countTotal(map));
		return lxDetail;
	}
	
	@GET
	@Path("/one")
	@Produces(MediaType.APPLICATION_JSON)
	public Nclx one(@QueryParam("id")String id){
		Nclx one = server.queryById(id);
		return one;
	}
}
